require("lsqlite3")
require("curl")
require("xml")

local VERBOSE, LANG, START, END, USE_IDS = true, "EN", 1, 35000
local URL, COOKIE
function handle_args(args)
	local nargs, i = #args, 1
	while i <= nargs do
		local arg = args[i]
		if arg == "-v" or arg == "--verbose" then
			VERBOSE = true
		elseif arg == "-l" or arg == "--lang" then
			i = i + 1
			LANG = args[i]:upper()
		elseif arg == "-s" or arg == "--start" then
			i = i + 1
			START = tonumber(args[i])
		elseif arg == "-e" or arg == "--start" then
			i = i + 1
			END = tonumber(args[i])
		elseif arg == "-i" or arg == "--id" then
			USE_IDS = true
		else
			print("Usage : "..args[0].." -(v,l,s,e,i)")
		end
		i = i + 1
	end
	if LANG == "US" then
		URL = "http://www.wowarmory.com"
	elseif LANG == "EN" then
		URL = "http://eu.wowarmory.com"
	elseif LANG == "FR" then
		URL = "http://eu.wowarmory.com"
		COOKIE = {"Cookie: cookieLangId=fr;"}
	end
end

local db = sqlite3.open("armory.db")
db:exec([[
CREATE TABLE IF NOT EXISTS cache (
	id INTEGER,
	type TEXT,
	lang TEXT,
	content BLOB,
	time TEXT DEFAULT CURRENT_TIMESTAMP,
	PRIMARY KEY (type, id, lang)
)]])

local get
do
	local temp
	local write = function (s, len)
		temp[#temp + 1] = s
		return len
	end
	function get(url)
		if VERBOSE then print("GET", url) io.flush() end
		temp = {}
		local c = curl.easy_init()
		c:setopt(curl.OPT_URL, url)
		c:setopt(curl.OPT_USERAGENT, "Firefox/2.0")
		if COOKIE then
			c:setopt(curl.OPT_HTTPHEADER, COOKIE)
		end
		c:setopt(curl.OPT_WRITEFUNCTION, write)
		local err, info = c:perform()
		if err ~= 0 then
			return
		else
			temp = table.concat(temp)
			if temp:len() > 0 then
				return temp
			end
		end
	end
end

local make_url = function (id, type)
	return URL.."/item-"..type..".xml?i="..id
end

local cached_get
do
	local select_stmt = db:prepare("SELECT content FROM cache WHERE id = ? AND type = ? AND lang = ?")
	local insert_stmt = db:prepare("INSERT INTO cache VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)")
	
	function cached_get(id, type)
		select_stmt:bind_values(id, type, LANG)
		local result = select_stmt:step()
		if result == sqlite3.ROW then
			result = select_stmt:get_value(0)
			select_stmt:reset()
			return result
		else
			select_stmt:reset()
		end
		local content = get(make_url(id, type))
		if content then
			insert_stmt:bind_values(id, type, LANG, content)
			insert_stmt:step()
			insert_stmt:reset()
		end
		return content
	end
end


local getiteminfo = function (id)
	return cached_get(id, "info")
end

local getitemtooltip = function (id)
	return cached_get(id, "tooltip")
end

function main()
	local ids
	if USE_IDS then
		local idb = assert(sqlite3.open("item_ids.db"))
		ids = assert(idb:prepare("SELECT COUNT(*) FROM itemids WHERE id = ?"))
	end
	local f, prev_percent = 0, 0
	local math_floor = math.floor
	local total = END - START + 1
	for i = START, END do
		local skip
		if USE_IDS then
			ids:bind_values(i)
			ids:step()
			skip = (ids:get_value(0) < 1)
			ids:reset()
		end
		if not skip then
			local t = getitemtooltip(i)
			if t then
				getiteminfo(i)
				f = f + 1
			end
			local percent = math_floor((i - START + 1) * 100 / total)
			if percent ~= prev_percent then
				print(percent, i, f)
				prev_percent = percent
			end
		end
	end
end

handle_args(arg)
main()
